package com.ujiuye.controller;

import com.ujiuye.pojo.Checkgroup;
import com.ujiuye.service.ICheckgroupService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 检查组 前端控制器
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Reference  //RPC 远程调用 代理模式
    private ICheckgroupService checkgroupService;

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            return checkgroupService.listPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_ADD')")
    public Result save(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.save(checkitemIds,checkgroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/{id}")//通过id查询检查项
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    public Result getById(@PathVariable("id") int id){
        try {
            return checkgroupService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_EDIT')")
    public Result update(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.update(checkitemIds,checkgroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_DELETE')")
    public Result remove(@PathVariable("id") int id){
        try {
            checkgroupService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    public Result listAll(){
        try {
            List<Checkgroup> list = checkgroupService.list();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}

